﻿using MetroFramework.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.IO;
using System.Threading;

namespace JoyConnector
{
    public partial class Form1 : MetroForm
    {
        #region 外部DLL调用
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        [DllImport("user32.dll")]
        private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        private const int WM_CLOSE = 0x0010;
        #endregion

        private string dataPath = System.Environment.CurrentDirectory + "\\data\\";
        private IntPtr hwnd;
        private Boolean IfDriverRunning = false;

        public Form1()
        {
            InitializeComponent();
        }

        public static bool IsWindows7
        {
            get
            {
                return (Environment.OSVersion.Platform == PlatformID.Win32NT) && (Environment.OSVersion.Version.Major == 6) && (Environment.OSVersion.Version.Minor == 1);
            }
        }

        public static bool IsUnderWindows7
        {
            get
            {
                return (Environment.OSVersion.Platform == PlatformID.Win32NT) && (Environment.OSVersion.Version.Major < 6);
            }
        }

        private void Form1_Shown(object sender, EventArgs e)
        {
            if(!IsWindows7)
            {
                metroButton3.Enabled = false;
                metroButton4.Enabled = false;
            }
            if(IsUnderWindows7)
            {
                MessageBox.Show(this,"程序不支持Win7以下版本", "错误");
                Application.Exit();
            }
        }

        private void metroButton1_Click(object sender, EventArgs e)
        {
            if(Environment.Is64BitOperatingSystem)
            {
                if (!File.Exists(dataPath + "cpp2017redistx64.exe"))
                {
                    MessageBox.Show("程序文件损坏", "错误");
                    return;
                }
                Process.Start(dataPath + "cpp2017redistx64.exe");
                metroButton1.Enabled = false;
            }
            else
            {
                if (!File.Exists(dataPath + "cpp2017redistx86.exe"))
                {
                    MessageBox.Show("程序文件损坏", "错误");
                    return;
                }
                Process.Start(dataPath + "cpp2017redistx86.exe");
                metroButton1.Enabled = false;
            }
        }

        private void metroButton2_Click(object sender, EventArgs e)
        {
            if (!File.Exists(dataPath + "vigembus.exe"))
            {
                MessageBox.Show("程序文件损坏", "错误");
                return;
            }
            Process.Start(dataPath + "vigembus.exe");
            metroButton2.Enabled = false;
        }

        private void metroButton3_Click(object sender, EventArgs e)
        {
            if (Environment.Is64BitOperatingSystem)
            {
                if (!File.Exists(dataPath + "\\winnt61\\Xbox360_64Eng.exe"))
                {
                    MessageBox.Show("程序文件损坏", "错误");
                    return;
                }
                Process.Start(dataPath + "\\winnt61\\Xbox360_64Eng.exe");
                metroButton3.Enabled = false;
            }
            else
            {
                if (!File.Exists(dataPath + "\\winnt61\\Xbox360_32Eng.exe"))
                {
                    MessageBox.Show("程序文件损坏", "错误");
                    return;
                }
                Process.Start(dataPath + "\\winnt61\\Xbox360_32Eng.exe");
                metroButton3.Enabled = false;
            }
        }

        private void metroButton4_Click(object sender, EventArgs e)
        {
            if (Environment.Is64BitOperatingSystem)
            {
                if (!File.Exists(dataPath + "\\winnt61\\Windows6.1-KB3033929-x64.msu"))
                {
                    MessageBox.Show("程序文件损坏", "错误");
                    return;
                }
                Process.Start(dataPath + "\\winnt61\\Windows6.1-KB3033929-x64.msu");
                metroButton4.Enabled = false;
            }
            else
            {
                if (!File.Exists(dataPath + "\\winnt61\\Windows6.1-KB3033929-x86.msu"))
                {
                    MessageBox.Show("程序文件损坏", "错误");
                    return;
                }
                Process.Start(dataPath + "\\winnt61\\Windows6.1-KB3033929-x86.msu");
                metroButton4.Enabled = false;
            }
        }

        private void metroButton5_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process p = new System.Diagnostics.Process();
            p.StartInfo.FileName = dataPath + "driver\\driver.exe";
            p.StartInfo.UseShellExecute = true;
            p.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
            p.Start();
            Thread.Sleep(200);
            IntPtr myIntPtr = FindWindow(null, dataPath + "driver\\driver.exe");
            hwnd = myIntPtr;
            IfDriverRunning = true;
            metroButton1.Enabled = false;
            metroButton2.Enabled = false;
            metroButton3.Enabled = false;
            metroButton4.Enabled = false;
            metroButton5.Enabled = false;
            notifyIcon1.Text = "JoyConnector\r\n状态：已启动";
            metroButton5.Text = "服务正在运行";
            this.WindowState = FormWindowState.Minimized;
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (IfDriverRunning)
                {
                    if (MessageBox.Show("手柄服务正在运行，关闭程序将导致手柄无法使用\r\n是否关闭？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                    {
                        endDriver();
                        notifyIcon1.Visible = false;
                    }
                    else
                        e.Cancel = true;
                }
            }
            catch
            {
                endDriver();
                notifyIcon1.Visible = false;
            }
        }

        private void endDriver()
        {
            SendMessage(hwnd, WM_CLOSE, 0, 0);
            IfDriverRunning = false;
            notifyIcon1.Text = "JoyConnector\r\n状态：未启动";
        }

        private void notifyIcon1_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (e.Button == MouseButtons.Left)
                {
                    if (this.WindowState == FormWindowState.Minimized)
                    {
                        this.Visible = true;
                        this.Activate();
                        this.WindowState = FormWindowState.Normal;
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if(IfDriverRunning)
            {
                if (MessageBox.Show("手柄服务正在运行，关闭程序将导致手柄无法使用\r\n是否关闭？", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
                    endDriver();
                    notifyIcon1.Visible = false;
                    Application.Exit();
                }
            }
            else
            {
                Application.Exit();
            }
        }

        private void Form1_SizeChanged(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Minimized)
            {
                this.Hide();
                this.notifyIcon1.Visible = true;
            }
        }

        private void metroLink1_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://gitee.com/elton11220");
        }

        private void metroLink2_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("https://github.com/elton11220");
        }
    }
}
